﻿(function($){
    var url = "/Handler/CommonHandler.ashx?action=GetUserInfo";
    var leftP = 0;
    var topP = 12;
    var ot = null;
    var s = "ui_card";
    
    function setLT(card, jq)
    {
        card.css({left: jq.offset().left + leftP, top: jq.offset().top + topP});
    }
    
    function hideCard(userID)
    {
        var p = $("#ui_card_" + userID);
        if(p.length > 0)
        {
            p.hide();
        }
        ot = null;
    }
    
    $.fn.extend({
        getPersonCard: function(userID, style){
            this.hover(function(){
                var card = $("#ui_card_" + userID);
                if(card.length > 0)
                {
                    setLT(card, $(this));
                    card.show();
                    return;
                }
                style = style == undefined ? s : style;
                
                var card = $("<div id='ui_card_" + userID + "' class='" + style + "'></div>");
                var html = [];
                html.push("<div class='ui_card_photo'></div>");
                html.push("<div class='ui_card_info'>");
                html.push("<div class='ui_card_name'><span class='ui_card_nm'>加载中...</span><span class='ui_card_id'></span></div>");
                html.push("<div class='ui_card_content'></div>");
                html.push("<div class='ui_card_91u'>91U</div>");
                html.push("<div class='ui_card_km'>KM</div>");
                html.push("</div>");
                card.html(html.join(""));
                setLT(card, $(this));
                card.hover(function(){
                    ot = clearTimeout(ot);
                }, function(){
                    hideCard(userID);
                });
                $(document.body).append(card);
                
                $.getJSON(url + "&r=" + Math.random(), {userID: userID}, function(json){
                    //加载完成
                    if(json.UserName == undefined)
                    {
                        card.find(".ui_card_name").html("加载失败");
                        return;
                    }
                    switch(style)
                    {
                        case "ui_card":
                        default:
                            card.find(".ui_card_photo").html("<img height='115' width='115' onload='DownImage(this,115,115,1,1)' src='" + json.Photo + "' alt='' onerror='this.src=\"lib/ui_card/images/default.png\"' />");
                            break;
                        case "ui_card_blue":
                            card.find(".ui_card_photo").html("<img height='106' width='81' onload='DownImage(this,81,106,1,1)' src='" + json.Photo + "' alt='' onerror='this.src=\"lib/ui_card/images/default.png\"' />");
                            break;
                    }
                    
                    card.find(".ui_card_nm").html(json.UserName);
                    card.find(".ui_card_id").html("("+json.UserID+")");
                    card.find(".ui_card_content").html("<span code='" + json.DeptCode + "'>" + json.DeptName + "</span><br/><span>" + json.WorkName + "</span>");
                    
                    //91U
                    card.find(".ui_card_91u").attr("title", "91U联系" + json.UserName).hover(
                        function(){
                            $(this).addClass("ui_card_91u_hover");
                        },
                        function(){
                            $(this).removeClass("ui_card_91u_hover").removeClass("ui_card_91u_active");
                        }
                    ).mousedown(function(){
                        $(this).addClass("ui_card_91u_active");
                    }).mouseup(function(){
                        $(this).removeClass("ui_card_91u_active");
                    }).click(function(){
                        window.open(json["91U"]);
                    });
                    
                    //KM
                    card.find(".ui_card_km").attr("title", "访问" + json.UserName + "的KM书桌").hover(
                        function(){
                            $(this).addClass("ui_card_km_hover");
                        },
                        function(){
                            $(this).removeClass("ui_card_km_hover").removeClass("ui_card_km_active");
                        }
                    ).mousedown(function(){
                        $(this).addClass("ui_card_km_active");
                    }).mouseup(function(){
                        $(this).removeClass("ui_card_km_active");
                    }).click(function(){
                        window.open(json.KM);
                    });
                });
            }, function(){
                if(!ot)
                {
                    ot = setTimeout(function(){
                        hideCard(userID);
                    }, 200);
                }
            });
        }
    });
})(jQuery);